import sys,glob,time,subprocess

def update(indir,jobid):
    runid = indir.split('/')[-2]

    while 1:
        kkfiles = glob.glob(f'{indir}/*.KK.xlsx')
        ntfiles = glob.glob(f'{indir}/*.NT.xlsx')
        kknum = len(kkfiles)
        ntnum = len(ntfiles)
        if ntnum == kknum:
            break
        time.sleep(120)
    
    script = ''
    if indir.split('/')[5] == 'guangzhou':
        script = f'''/data/mngsSYS/b/venv/bin/python /data/mngsSYS/b/app/dbCU/SampleAIUpdate.py /data/pub/today/guangzhou/{runid}/kkbwa/GZ.autoTest.$(date +%Y%m%d).xlsx {jobid}'''
    elif indir.split('/')[5] == 'fuzhou':
        script = f'''/data/mngsSYS/b/venv/bin/python /data/mngsSYS/b/app/dbCU/SampleAIUpdate.py /data/pub/today/fuzhou/{runid}/kkbwa/FZ.autoTest.$(date +%Y%m%d).xlsx {jobid}'''
    elif indir.split('/')[5] == 'zhengzhou':
        script = f'''/data/mngsSYS/b/venv/bin/python /data/mngsSYS/b/app/dbCU/SampleAIUpdate.py /data/pub/today/zhengzhou/{runid}/kkbwa/ZZ.autoTest.$(date +%Y%m%d).xlsx {jobid}'''
    elif indir.split('/')[5] == 'changsha':
        script = f'''/data/mngsSYS/b/venv/bin/python /data/mngsSYS/b/app/dbCU/SampleAIUpdate.py /data/pub/today/changsha/{runid}/kkbwa/CS.autoTest.$(date +%Y%m%d).xlsx {jobid}'''
    elif indir.split('/')[5] == 'beijing':
        script = f'''/data/mngsSYS/b/venv/bin/python /data/mngsSYS/b/app/dbCU/SampleAIUpdate.py /data/pub/today/beijing/{runid}/kkbwa/BJ.autoTest.$(date +%Y%m%d).xlsx {jobid}'''
    elif indir.split('/')[5] == 'shanghai':
        script = f'''/data/mngsSYS/b/venv/bin/python /data/mngsSYS/b/app/dbCU/SampleAIUpdate.py /data/pub/today/shanghai/{runid}/kkbwa/SH.autoTest.$(date +%Y%m%d).xlsx {jobid}'''
    subprocess.run(script,shell=True)

if __name__ == '__main__':
    inputdir = sys.argv[1]
    jobid = sys.argv[2]
    update(inputdir,jobid)
